 function locdau(str) {  
		  str= str.toLowerCase();  
		  str= str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g,"a");  
		  str= str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g,"e");  
		  str= str.replace(/ì|í|ị|ỉ|ĩ/g,"i");  
		  str= str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g,"o");  
		  str= str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g,"u");  
		  str= str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g,"y");  
		  str= str.replace(/đ/g,"d");  
		  str= str.replace(/!|@|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|\/|,|\.|\:|\;|\'| |\"|\&|\#|\[|\]|~|$|_/g,"-"); 
		  str= str.replace(/-+-/g,"-"); //thay thế 2- thành 1- 
		  str= str.replace(/^\-+|\-+$/g,"");  
		  return str;  
	}
function replace_string(search, replace, str){
		var ra = replace instanceof Array, sa = str instanceof Array, l = (search = [].concat(search)).length, replace = [].concat(replace), i = (str = [].concat(str)).length;
		while(j = 0, i--)
			while(str[i] = str[i].split(search[j]).join(ra ? replace[j] || "" : replace[0]), ++j < l);
		return sa ? str : str[0];
	}
function do_search() {
	var kw = $('#viewbtn').val();
	kw = replace_string("'", '&#039;', kw);
	kw = replace_string(' ', '-', kw);
	kw = replace_string('%20', '-', kw);
	if (!kw){ alert('Bạn chưa nhập từ khóa');
	}else {
		kw = locdau(kw);
		window.location.href =domain_url+'/tim-kiem/'+kw+'/';	
	}
  return false;
}

$(document).ready(function(){
	$(window).resize(function(){
		tinhautosear();
		$(".akey1").css({ "width":eval($("body").width()-100)});
	});
	/*---------------------------------------------------------------*/
	function tinhautosear(){
		$(".search-autocomplete").css({ "width":eval($(window).width() -100 + 42 -2)});
		//$("..search-item ul").css("width",eval($(".search-autocomplete").width()-63));
	}
	tinhautosear();
	/*---------------------------------------------------------------*/
	$('#viewbtn').click(function() {        	     		 
		 var b111= $('#viewbtn').val();
		 if(b111.length >= 3){
			 $("#mp-autocomplete-results").css("display","block");      			 
			var te12=$(".keyshow").attr("sasdata");
			te12.replace('"',"");b111.replace('"',"");
			 if(te12 != b111){
				 $('#viewbtn').keyup(); 
			 }
		 }
	});
	$('#viewbtn').change(function() { $('#viewbtn').keyup(); });
	/*---------------------------------------------------------------*/
	$('body').click(function(e){
		if(! $(e.target).hasClass('viewbtn')){
			$("#mp-autocomplete-results").css("display","none");
		}
	});
	/*---------------------------------------------------------------*/
	
	/*---------------------------------------------------------------*/
	
	$('#viewbtn').keyup(function() {
		var keyword = $('#viewbtn').val();
		if(keyword.length >= 3){
			$(".imgload3").css("display","block");
			$("#mp-autocomplete-results").css("display","block");
			$(".keyshow").html('"' + keyword +'"').attr("sasdata",keyword);	
			keyword = locdau(keyword);
			$(".keyshow").parent().attr("href",domain_url+"/tim-kiem/"+ keyword+"/" );	
			if(request){
				request.abort();
			}
		   var request =$.ajax({
				url: domain_url+"/search.php",
				type: 'POST',
				cache: false,
				data: 'keyword=' + keyword,
				success: function(string){
					var getData =JSON.parse(string);
					var html="";
					if(getData){
						for (var key in getData) {
							html += '<li class=""><a href="'+ getData[key].linkphim +'"><img src="'+ getData[key].img +'" alt="" class="search-img"><strong>'+ getData[key].etitle +'</strong><strong class="strong2">'+ getData[key].title +'</strong></a></li>';
							$(".search1").html(html);
						}
					}else{
						$(".search0").css("display","none");
					}
				},
				error: function (){
				}
			}); 
			$(".imgload3").css("display","none");
		}
	});
	/*---------------------------------------------------------------*/
}); 
